package com.xiaotu.spider.maoyan.pageprocessor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.MaoYanMovieBasicInfoModel;
import com.xiaotu.common.model.MaoYanMovieRecordOnlineModel;
import com.xiaotu.common.util.CacheHandler;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.maoyan.MaoYanMovieBasicInfoService;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;

@Component("MaoYanMovieSupplementCheckOnlinePageProcessor")
public class MaoYanMovieSupplementCheckOnlinePageProcessor extends AUrlPageProcessor {

	private String [] LIST_URL = {
			"https://box.maoyan.com/promovie/api/box/second.json?beginDate=%s"
	};
	
	private String dataUrl = "https://box.maoyan.com/promovie/api/box/second.json?beginDate=%s";
	
    private String startDate = null;
	
	private String endDate = null;
    
    @Autowired
    protected CacheHandler cacheHandler;
    
    @Autowired
    private MaoYanMovieBasicInfoService maoYanMovieBasicInfoService;
    
    @Override
    public void initRunParams(Map<String, Object> map)
    {
        if(map.get("startDate") != null)
        	this.startDate = map.get("startDate").toString();
        if(map.get("endDate") != null)
        	this.endDate = map.get("endDate").toString();
    }
    
    
    private String convertDateToNum(String str){
    	return str.replaceAll("[-]", "");
    }
    
    private String convertDateToDate(String str){
    	String first = str.substring(0,4);
    	String second = str.substring(4, 6);
    	String three = str.substring(6);
    	return first+"-"+second+"-"+three;
    }
    
    
	@SuppressWarnings("unchecked")
	@Override
	public void process(Page page) {
		
		String url = page.getRequest().getUrl();
		
		//获取今天的日期
		String currTodayNum = url.substring(url.lastIndexOf("=")+1);
		
		String currTodayDate = convertDateToDate(currTodayNum);
		
		String currYesTodayNum = DateUtils.getSpecifiedDayBeforeDate(currTodayDate,DateUtils
	            .FILE_NOMENCLATURE_FORMAT);
		
		String currYesTodayDate = convertDateToDate(currYesTodayNum); 
		
		//获取明天日期
		String tomorrowNum = DateUtils.getSpecifiedDayAfterDate(currTodayDate,DateUtils
	            .FILE_NOMENCLATURE_FORMAT);
		
		
		if(currTodayNum.compareTo(convertDateToNum(endDate))<=0){
			
			List<MaoYanMovieBasicInfoModel> yDayList = new ArrayList<MaoYanMovieBasicInfoModel>();
		    
			List<MaoYanMovieBasicInfoModel> nDayList = new ArrayList<MaoYanMovieBasicInfoModel>();
		    
		    List<MaoYanMovieBasicInfoModel> newOnlineList = new ArrayList<MaoYanMovieBasicInfoModel>();
			
			String json = page.getRawText();
	        if (StringUtils.isEmpty(json))
	            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
	        Map<String, Object> map = GsonUtils.fromJson(json, Map.class);
	        if (map == null || map.isEmpty() || map.get("data") == null)
	            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
			Map<String, Object> dataMap = ((Map<String, Object>) map.get("data"));
	        
			List<Map<String, Object>> todayDataList = (List<Map<String, Object>>) dataMap.get("list");
			
			String yesTodayJsonObj = HttpUtils.httpGetJSONP(String.format(dataUrl, convertDateToNum(currYesTodayDate)));
			JSONObject object = JSONObject.fromObject(yesTodayJsonObj);
			JSONArray jsonArray = object.getJSONObject("data").getJSONArray("list");
			
			List<Map<String,Object>> yesterdayDataList = jsonArray;
			
			List<MaoYanMovieRecordOnlineModel> maoYanMovieRecordOnlineModelList = new ArrayList<MaoYanMovieRecordOnlineModel>();
			
			
			
			
			for(Map<String,Object> mov : todayDataList){
		    	MaoYanMovieBasicInfoModel basicInfo = new MaoYanMovieBasicInfoModel();
		    	String dataId = mov.get("movieId") == null?"":RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, StringUtil
		                .nullToString(mov.get("movieId")));
		    	basicInfo.setDataid(dataId);
		    	nDayList.add(basicInfo);
	        }
			for(Map<String,Object> mov : yesterdayDataList){
		    	MaoYanMovieBasicInfoModel basicInfo = new MaoYanMovieBasicInfoModel();
		    	String dataId = mov.get("movieId") == null?"":RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, StringUtil
		                .nullToString(mov.get("movieId")));
		    	basicInfo.setDataid(dataId);
		    	yDayList.add(basicInfo);
	        }
		    if(!yDayList.isEmpty() && yDayList.size()>0 && !nDayList.isEmpty() && nDayList.size()>0){
		    	for(MaoYanMovieBasicInfoModel yModel : yDayList){
		    		boolean temp = false;
		    		for(MaoYanMovieBasicInfoModel nModel : nDayList){
		    			if(yModel.getDataid().equals(nModel.getDataid())){
		    				temp = true;
		    			}
		    		}
		    		if(!temp){
		    			yModel.setStatus(1);//已下线
		    			yModel.setOfflineDate(currYesTodayNum);//昨天下线
		    		}else{
		    			yModel.setStatus(0);//未下线
		    			yModel.setOfflineDate(null);//将之前的下线记录清除
		    		}
		    	}
		    	
		    	//确定新上线
		    	for(MaoYanMovieBasicInfoModel nModel : nDayList){
		    		boolean temp2 = false;
		    		for(MaoYanMovieBasicInfoModel yModel : yDayList){
		    			if(yModel.getDataid().equals(nModel.getDataid())){
		    				temp2 = true;
		    			}
		    		}
		    		if(!temp2){
		    			newOnlineList.add(nModel);
		    		}
	    		}
		    	
		    	yDayList.addAll(nDayList);
		    	List<MaoYanMovieBasicInfoModel> resultList = maoYanMovieBasicInfoService.queryMovieBasicInfoList(yDayList);
		    	yDayList.removeAll(nDayList);
		    	for(int i =0;i<resultList.size();i++){
		    		MaoYanMovieBasicInfoModel tempModel = resultList.get(i);
		    		for(MaoYanMovieBasicInfoModel yModel : yDayList){
		    			if(tempModel.getDataid().equals(yModel.getDataid())){
		    				if(yModel.getStatus()==1){
		    					tempModel.setStatus(1);
		    					tempModel.setOfflineDate(currYesTodayNum);//昨天下线
		    					
		    					MaoYanMovieRecordOnlineModel maoYanMovieRecordOnlineModel = new MaoYanMovieRecordOnlineModel(
		    							tempModel.getId(),
		    							tempModel.getTvname(),
		    							tempModel.getDataid(),
		    							Constants.SiteName.MAOYAN,
		    							tempModel.getOnlineDate(),
		    							currYesTodayNum,
		    							null
		    	    					);
		    					
		    					maoYanMovieRecordOnlineModelList.add(maoYanMovieRecordOnlineModel);
		    					
		    				}else if(yModel.getStatus()==0){
		    					tempModel.setStatus(0);
		    					//确定之前是否存在下线记录 
		    					if(null != tempModel.getOfflineDate() && currTodayNum.compareTo(tempModel.getOfflineDate())>0){
		    						tempModel.setOnlineDate(currTodayNum);
		    					}
		    					tempModel.setOfflineDate(null);//将之前的下线记录清除
		    				}
		    			}
		    		}
		    		for(MaoYanMovieBasicInfoModel nOnlieModel : newOnlineList){
		    			if(tempModel.getDataid().equals(nOnlieModel.getDataid())){
	    					tempModel.setStatus(0);
	    					tempModel.setOnlineDate(currTodayNum);
	    					tempModel.setOfflineDate(null);//将之前的下线记录清除
		    			}
		    		}
		    	}
		    	
		    	maoYanMovieBasicInfoService.updateBatchMovieBasicInfo(resultList,maoYanMovieRecordOnlineModelList);
		    	page.addTargetRequest(String.format(dataUrl,tomorrowNum));
		    	
		    }
		}
		
	}

	@Override
	protected String[] getStartRequestUrls() {
		return new String[]{String.format(LIST_URL[0], convertDateToNum(startDate))};
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}

	@Override
	public String getPageDataType(Page page) {
        return Constants.PageDataType.SEARCH_LIST;
	}
	
}
